/**
 * Copyright (c) 2010, "Shashank Tiwari"
 * web: www.shanky.org | www.treasuryofideas.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the fireclay-core project nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Created: Jun 3, 2010
 */

package fireclay.logging
{
	import flash.utils.getQualifiedClassName;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.targets.LineFormattedTarget;
	import mx.logging.targets.TraceTarget;


	public class LogManager implements ILogManager
	{
		private static var _instance:LogManager=LogManager.getInstance();
		private static var allowInstantiation:Boolean;

		public function LogManager()
		{
			if (!allowInstantiation)
			{
				throw new Error("Error: Instantiation failed. Use LogManager.getInstance() to get an instance of LogManager.");
			}
		}

		public static function getInstance():LogManager
		{
			if (!_instance)
			{
				allowInstantiation=true;
				_instance=new LogManager();
				allowInstantiation=false;
			}
			return _instance;
		}

		private function setLogTargetDefaults(logTarget:LineFormattedTarget):LineFormattedTarget
		{
			logTarget.filters=LogTargetDefaults.FILTERS;
			logTarget.fieldSeparator=LogTargetDefaults.FIELD_SEPARATOR;
			logTarget.level=LogTargetDefaults.LEVEL;
			logTarget.includeDate=LogTargetDefaults.INCLUDE_DATE;
			logTarget.includeTime=LogTargetDefaults.INCLUDE_TIME;
			logTarget.includeCategory=LogTargetDefaults.INCLUDE_CATEGORY;
			logTarget.includeLevel=LogTargetDefaults.INCLUDE_LEVEL;

			return logTarget;

		}


		public function getLogger(name:*):ILogger
		{
			var myLogger:ILogger;
			if (name is String)
			{

				myLogger=getLoggerByCategoryName(name);
			}
			else if (name is Class)
			{
				myLogger=getLoggerByClass(name);
			}
			else
			{
				myLogger=null;
				throw new Error("Error: Incorrect data type. Please pass in a category name or a Class.");
			}
			return myLogger;
		}

		private function getLoggerByCategoryName(categoryName:String):ILogger
		{
			return Log.getLogger(categoryName);
		}

		private function getLoggerByClass(c:Class):ILogger
		{
			var className:String=getQualifiedClassName(c).replace("::", ".");
			return Log.getLogger(className);
		}

		public function getConfiguredLogTarget(logTarget:LineFormattedTarget, logTargetConfig:LogTargetConfig=null):LineFormattedTarget
		{
			if (logTargetConfig != null)
			{
				logTarget=configureLogTarget(logTarget, logTargetConfig);
			}
			else
			{
				logTarget=setLogTargetDefaults(logTarget);
			}

			return logTarget;
		}

		private function configureLogTarget(logTarget:LineFormattedTarget, logTargetConfig:LogTargetConfig):LineFormattedTarget
		{
			logTarget.filters=logTargetConfig.filters;
			logTarget.fieldSeparator=logTargetConfig.fieldSeparator;
			logTarget.level=logTargetConfig.level;
			logTarget.includeDate=logTargetConfig.includeDate;
			logTarget.includeTime=logTargetConfig.includeTime;
			logTarget.includeCategory=logTargetConfig.includeCategory;
			logTarget.includeLevel=logTargetConfig.includeLevel;

			return logTarget;
		}

		public function getDefaultTraceTarget():TraceTarget
		{
			var traceTarget:TraceTarget=new TraceTarget();
			return setLogTargetDefaults(traceTarget as LineFormattedTarget) as TraceTarget;
		}

		public function getConfiguredTraceTarget(logTargetConfig:LogTargetConfig):TraceTarget
		{
			var traceTarget:TraceTarget=new TraceTarget();
			return configureLogTarget(traceTarget as LineFormattedTarget, logTargetConfig) as TraceTarget;
		}

	}
}